import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of, Subject } from 'rxjs';
import { catchError, filter, map } from 'rxjs/operators';
import { SERVER_API_URL, USER_API_URL } from 'app/app.constants';
// import { Equipment, Result, Page, EquipmentType } from './interface/interface';
// import { LaneGroup } from 'app/public';
// import { ToolsService } from 'app/share';
// import { JUNCTIONM_API_URL, EQUIPMENTM_API_URL } from 'app/app.constants';
import { Router } from '@angular/router';
import { ManageUser, ManageRole, ManageOrganization } from './interface/interface';
import { ConfigService } from 'app/config/config.service';

@Injectable({
    providedIn: 'root'
})
export class UserManagementService {
    private apiUrl_equip_online = this.config.apiUrl.user;
    private apiUrl_enum_online = this.config.apiUrl.enum;
    // private apiUrl_equip_online = USER_API_URL;
    // private handleError: HandleError;

    private _userConfigData: any;
    configDataExsited = false;

    get userConfigData() {
        return this._userConfigData;
    }

    set userConfigData(data) {
        this._userConfigData = data;
    }

    constructor(private http: HttpClient, private router: Router, private config: ConfigService) {}
    /**
     * get lane config info
     * @param params { juncId: string , isTemp: 0temp??1offcial }
     */
    // 获取用户列表
    getAllUser(param): Observable<any> {
        // const url = `${this.apiUrl_equip_online}/userMgt/user/getAllUser`;
        return this.http.post<any>(this.apiUrl_equip_online + '/userMgt/user/getAllUserInfoByPage', param);
    }
    // 获取road和site下拉框数据
    getAllEnumInfo(): Observable<any> {
        const url = `${this.apiUrl_enum_online}/vnqnenummgt/user/getAllEnumInfo`; // 查询参数
        return this.http.get<any>(url);
    }
    // 创建用户
    createUserInfo(userInfo): Observable<ManageUser> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/createUserInfo`;
        return this.http.post<ManageUser>(url, userInfo);
    }

    // 更新用户
    updateUserInfo(userInfo): Observable<ManageUser> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/updateUserInfo`;
        return this.http.post<ManageUser>(url, userInfo);
    }

    // 删除用户
    deleteUserInfo(userInfo): Observable<ManageUser> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/deleteUserInfo`;
        return this.http.post<ManageUser>(url, userInfo);
    }

    // 获取角色列表
    getAllUserRole(param): Observable<any> {
        // const url = `${this.apiUrl_equip_online}/userMgt/user/getAllUserRoleByPage`;
        return this.http.post<any>(this.apiUrl_equip_online + '/userMgt/user/getAllUserRoleByPage', param);
    }

    // 新建角色
    createUserRole(userInfo): Observable<ManageRole> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/createUserRole`;
        return this.http.post<ManageRole>(url, userInfo);
    }

    // 编辑角色
    updateUserRole(userInfo): Observable<ManageRole> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/updateUserRole`;
        return this.http.post<ManageRole>(url, userInfo);
    }

    // 删除角色
    deleteUserRole(userInfo): Observable<any> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/deleteUserRole`;
        return this.http.post<any>(url, userInfo);
    }

    // 获取organization
    getAllOrganizations(param): Observable<any> {
        // const url = `${this.apiUrl_equip_online}/userMgt/user/getAllOrganizationByPage`;
        return this.http.post<any>(this.apiUrl_equip_online + '/userMgt/user/getAllOrganizationByPage', param);
    }

    // 新建organization
    createOrganization(userInfo): Observable<ManageOrganization> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/createOrganization`;
        return this.http.post<ManageOrganization>(url, userInfo);
    }

    // 编辑organization
    updateOrganization(userInfo): Observable<ManageOrganization> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/updateOrganization`;
        return this.http.post<ManageOrganization>(url, userInfo);
    }

    // 删除organization
    deleteOrganization(oid): Observable<any> {
        const url = `${this.apiUrl_equip_online}/userMgt/user/deleteOrganization?organization_Ids=${oid}`; // 查询参数
        // const url = `${this.apiUrl_equip_online}/userMgt/user/deleteOrganization/${oid}`; // 路径参数
        return this.http.get<any>(url);
    }
    // get tree data
    getRoleTreeData(data) {
        // 不需要配置的权限
        const no_rightName = ['Security_Management', 'Incident_Logs', 'Manage_Access_Rights', 'PRINT', 'export', 'Look', 'GIS'];
        const url = `${this.apiUrl_equip_online}/userMgt/user/getAllAccessRight`;
        const _ss = new Subject<any>();
        this.http.post<any>(url, data).subscribe(res => {
            const newList = [];
            res.content.content.forEach((item, index) => {
                let has = false;
                no_rightName.forEach(right => {
                    if (right === item.rightName && item.accessId !== 6) {
                        // 排除一级菜单的GIS
                        has = true;
                    }
                });
                if (!has) {
                    newList.push(item);
                }
            });
            res.content.content = newList;
            _ss.next(res);
        });
        return _ss.asObservable();
    }

    getAllAccessRight(data) {
        // 不需要配置的权限
        const url = `${this.apiUrl_equip_online}/userMgt/user/getAllAccessRight`;
        return this.http.post<any>(url, data);
    }

    /**
     * equipment bind
     * @param params { Equipment }
     */

    private handleError<T>(operation = 'operation', result?: T) {
        return (error: any): Observable<T> => {
            // TODO: send the error to remote logging infrastructure
            console.error(error); // log to console instead

            // TODO: better job of transforming error for user consumption
            this.log(`${operation} failed: ${error.message}`);

            // Let the app keep running by returning an empty result.
            return of(result as T);
        };
    }
    private log(message: string) {}
}
